﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Objects;
using BAL;
using System.IO;
using System.Collections.Generic;

public partial class Admin_UCControls_MauThe_Update : System.Web.UI.UserControl
{
    public static string srtPath;

    public static MauThe ItemMauThe;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllChuDe();
            int idMauThe;
            try
            {
                idMauThe = Convert.ToInt16(Request.QueryString["IdMauThe"].ToString());
            }
            catch
            {
                idMauThe = -1;
            }


            ItemMauThe = new MauThe();

            if (idMauThe > 0)
            {
               // ItemMauThe = new MauThe();
                idMauThe = Convert.ToInt16(Request.QueryString["IdMauThe"].ToString());

                ItemMauThe = BAL.BALMauThe.GetById(idMauThe);


                ddlChuDe.SelectedValue = ItemMauThe.IdChuDe.ToString();

                txtTenMauThe.Text = ItemMauThe.TenMauThe.ToString();
                txtMoTa.Text = ItemMauThe.GhiChu.ToString();

                string pathfile = PathImage();

                imgMauThe.ImageUrl = pathfile + ItemMauThe.DuongDan;
            }
           

        }

    }

    public void GetAllChuDe()
    {
        List<ChuDe> ListChuDe = new List<ChuDe>();
        ListChuDe = BAL.BALChuDe.GetAllItem();

        ChuDe item = new ChuDe();

        item.Id = 0;
        item.TenChuDe = "-- Chọn Chủ đề --";

        ListChuDe.Insert(0, item);

        ddlChuDe.DataTextField = "TenChuDe";
        ddlChuDe.DataValueField = "Id";
        ddlChuDe.DataSource = ListChuDe;
        ddlChuDe.DataBind();
    }

    public string PathImage()
    {
        return srtPath = "~/images/Generic Card/MauThe";
    }


    protected void btnLuu_Click(object sender, EventArgs e)
    {
        int item = 0;

        if (CheckNull())
        {
            if (ItemMauThe.Id == 0)
            {
                if (FUDuongDan.HasFile)
                {
                    if (CheckFileType(FUDuongDan.FileName))
                    {
                        srtPath = PathImage() + FUDuongDan.FileName;

                        FUDuongDan.SaveAs(MapPath(srtPath));


                        item = BALMauThe.Insert_MauThe(ddlChuDe.SelectedIndex, txtTenMauThe.Text, FUDuongDan.FileName.ToString(), txtMoTa.Text, 1, 0);
                        lblTenFile.Visible = true;
                        lblTenFile.Text = "File Uploaded: " + FUDuongDan.FileName;
                        imgMauThe.ImageUrl = srtPath;
                    }
                    else
                    {
                        lblTenFile.Visible = true;
                        lblTenFile.Text = "File Uploaded: " + " Invalid format.";
                    }

                }
                else
                {
                    lblTenFile.Visible = true;
                    lblTenFile.Text = "No File Uploaded.";
                }

            }
            else
            {
                // kiem tra co doi hinh hay ko, hay doi chu de
                if (!FUDuongDan.FileName.Equals(""))
                {
                    item = BALMauThe.Update_MauThe(Int32.Parse(ItemMauThe.Id.ToString()), Convert.ToInt16(ddlChuDe.SelectedValue), txtTenMauThe.Text, FUDuongDan.FileName.ToString(), txtMoTa.Text, 1, Int32.Parse(ItemMauThe.SapXep.ToString()));
                    lblTenFile.Visible = true;
                    lblTenFile.Text = "File Uploaded: " + ItemMauThe.DuongDan.ToString();
                    imgMauThe.ImageUrl = srtPath;

                }
                else
                {
                    item = BALMauThe.Update_MauThe(Int32.Parse(ItemMauThe.Id.ToString()), Convert.ToInt16(ddlChuDe.SelectedValue), txtTenMauThe.Text, ItemMauThe.DuongDan.ToString(), txtMoTa.Text, 1, Int32.Parse(ItemMauThe.SapXep.ToString()));
                    imgMauThe.ImageUrl = PathImage() + ItemMauThe.DuongDan.ToString();
                }

            }
        }

        if (item > 0)
        {
            lblThongbao.Visible = true;
            lblThongbao.Text = "Cập nhật thành công";

        }
        else
        {
            lblThongbao.Visible = true;
            lblThongbao.Text = "Cập nhật thất bại";
        }
    }

    bool CheckFileType(string fileName)
    {

        string ext = Path.GetExtension(fileName);

        switch (ext.ToLower())
        {

            case ".gif":

                return true;

            case ".png":

                return true;

            case ".jpg":

                return true;

            case ".jpeg":

                return true;

            default:

                return false;

        }

    }


    protected void btnQuayve_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Admin/MauThe.aspx");
    }

    public bool CheckNull()
    {
        bool flag = true;
        VisibleLable();

        if (ddlChuDe.SelectedIndex == 0)
        {
            lblTenChuDe.Visible = true;
            ddlChuDe.Focus();
            flag = false;
        }

        if (txtTenMauThe.Text == "")
        {
            lblMauThe.Visible = true;
            txtTenMauThe.Focus();
            flag = false;
        }

        return flag;
    }

    public void VisibleLable()
    {
        lblTenChuDe.Visible = false;
        lblMauThe.Visible = false;
    }
}
